package com.pkx.guide.system.controller;

import com.pkx.guide.common.annotation.ORpose;
import com.pkx.guide.system.model.query.DeptQuery;
import com.pkx.guide.system.model.vo.DeptVO;
import com.pkx.guide.common.annotation.PreventDuplicateSubmit;
import com.pkx.guide.common.model.Option;
import com.pkx.guide.system.model.form.DeptForm;
import com.pkx.guide.system.service.TSysDeptService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 部门控制器
 *
 * @author haoxr
 * @since 2020/11/6
 */
@Tag(name = "部门接口")
@RestController
@RequestMapping("/dept")
@ORpose
public class TSysDeptController {

    @Resource
    private TSysDeptService deptService;

    @Operation(summary = "获取部门列表")
    @GetMapping
    public List<DeptVO> listDepartments(@ParameterObject DeptQuery queryParams) {
        return deptService.listDepartments(queryParams);
    }

    @Operation(summary = "获取部门下拉选项")
    @GetMapping("/options")
    public List<Option> listDeptOptions() {
        return deptService.listDeptOptions();
    }

    @Operation(summary = "获取部门表单数据")
    @GetMapping("/{deptId}/form")
    public DeptForm getDeptForm(@Parameter(description ="部门ID") @PathVariable Long deptId) {
        return deptService.getDeptForm(deptId);
    }

    @Operation(summary = "新增部门")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:dept:add')")
    @PreventDuplicateSubmit
    public Long saveDept(@Valid @RequestBody DeptForm formData) {
        return deptService.saveDept(formData);
    }

    @Operation(summary = "修改部门")
    @PutMapping(value = "/{deptId}")
    @PreAuthorize("@ss.hasPerm('sys:dept:edit')")
    public Long updateDept(@PathVariable Long deptId, @Valid @RequestBody DeptForm formData) {
        return deptService.updateDept(deptId, formData);
    }

    @Operation(summary = "删除部门")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:dept:delete')")
    public Boolean deleteDepartments(@Parameter(description ="部门ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids) {
        return deptService.deleteByIds(ids);
    }
}
